import { createStore } from 'vuex'
import {login} from '../assets/api/login'
import { routers } from "../router/menus";
// console.log(routers,'routersrouters')
export default createStore({
  state: {
     token:sessionStorage.getItem('yunToken')?sessionStorage.getItem('yunToken'):'',
     isopen:false,//侧边是展开还是关闭
     menus:[],// 路由存放
     isLogin:false,//是不是login
     sidberIndex:0,//侧边的数据选择
     sidberList:[],//点击上面侧边显示的菜单栏
  },
  mutations: {
    SET_TOKEN:(state,token:string)=>{//设置token
        state.token = token 
    },
    SET_ISOPEN:(state,isopen:boolean)=>{//侧边收缩
      state.isopen = isopen 
    },
    SET_MENUS:(state,menus:[])=>{  //路由
      state.menus = menus 
    },
    SET_ISLOGIN:(state,isLogin:boolean)=>{  //是不是login
      state.isLogin = isLogin 
    },
    SET_SIDBERINDEX:(state,sidberIndex:number)=>{  //侧边的数据选择
      state.sidberIndex = sidberIndex 
    },
    SET_SIDBERLIST:(state,sidberList:[])=>{  //击上面侧边显示的菜单栏
      state.sidberList = sidberList 
    }
  },
  actions: {
    //击上面侧边显示的菜单
    SIDBERLIST({ commit },arr:[]){
      commit('SET_SIDBERLIST', arr)
    },
    //侧边的数据选择
    SIDBERINDEX({ commit },sidberIndex:number){
      commit('SET_SIDBERINDEX', sidberIndex)
    },
      // 登录
      Login ({ commit }, userInfo:object) {
        return new Promise((resolve, reject) => {
          login(userInfo).then(res => {
            const tokenStr = res.data.data.tokenHead + res.data.data.token;
            sessionStorage.setItem('yunToken',tokenStr);
            commit('SET_TOKEN', tokenStr)
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      ////侧边是展开还是关闭
      SiderOpen({commit},isopen){
        commit('SET_ISOPEN', isopen)
      },
      ////是不是login页面
      IsLogin({commit},isLogin){
        commit('SET_ISLOGIN', isLogin)
      },
      //路由
      Menus({commit},menus:any){
        
        return new Promise((resolve, reject) => {
          // login(userInfo).then(res => {
            commit('SET_ISOPEN',menus)
            resolve(routers)
          // }).catch(error => {
          //   reject(error)
          // })
        })
        

      }

  },
  modules: {
  },
})
